package cn.sh.yhk.service.basic.impl;

import cn.sh.yhk.mapper.basic.SMenuMapper;
import cn.sh.yhk.mapper.basic.SRoleMapper;
import cn.sh.yhk.model.basic.SMenu;
import cn.sh.yhk.model.basic.SRole;
import cn.sh.yhk.model.basic.SRoleExample;
import cn.sh.yhk.service.basic.RoleService;
import cn.sh.yhk.vo.BasicVo;
import cn.sh.yhk.vo.SRoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: adminyhk
 * @Date: 2018/9/25 15:17
 * @Description:
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    SRoleMapper sRoleMapper;
    @Resource
    SMenuMapper sMenuMapper;


    @Override
    public BasicVo getList(Long userId) {
        List<SRoleVo> sRoleVos = new ArrayList<SRoleVo>();
        List<SRole> sRoles = sRoleMapper.selectByExample(new SRoleExample());
        List<SRole> roles = sRoleMapper.selectRoleByUserId(userId);
        for (SRole psrole : sRoles) {
            SRoleVo sRoleVo = new SRoleVo();
            BeanUtils.copyProperties(psrole, sRoleVo);
            if (roles.size() > 0) {
                for (SRole sRole : roles) {
                    if (psrole.getId().equals(sRole.getId())) {
                        sRoleVo.setContains(true);
                    }
                    sRoleVo.setMenuList(
                            sMenuMapper.selectMenuByRoleId(sRole.getId().longValue()).stream().sorted(Comparator.comparing(SMenu::getMenuSort)).collect(Collectors.toList()));
                    sRoleVos.add(sRoleVo);
                }
            } else {
                sRoleVo.setContains(false);
                sRoleVos.add(sRoleVo);
            }
        }
        return BasicVo.success(sRoleVos.stream().sorted(Comparator.comparing(SRoleVo::getId)).collect(Collectors.toList()));
    }

    @Override
    public BasicVo createRole(SRole sRole) {
        SRoleExample ex = new SRoleExample();
        ex.createCriteria().andGroupNameEqualTo(sRole.getRoleName());
        if (sRoleMapper.selectByExample(ex).size() > 0) {
            return BasicVo.fail("角色重复");
        } else {
            int i = sRoleMapper.insertSelective(sRole);
            if (i > 0) {
                return BasicVo.success("请求成功");
            } else {
                return BasicVo.fail("请求失败");
            }
        }
    }

    @Override
    public BasicVo deleteRole(Long roleId) {
        if (roleId != null && roleId != 1l) {
            return BasicVo.fail("保存失败,groupId为空！");
        }
        SRoleExample ex = new SRoleExample();
        ex.createCriteria().andIdEqualTo(roleId.intValue());
        sRoleMapper.deleteByExample(ex);
        return BasicVo.success("保存成功");
    }

    @Override
    public BasicVo getList() {
        List<SRole> list = sRoleMapper.selectByExample(new SRoleExample());
        if (list.size() > 0) {
            return BasicVo.success("请求成功", list);
        } else {
            return BasicVo.fail("请求失败");
        }
    }
}
